import 'package:flutter/material.dart';
import 'package:flutter_2/providers/AtUserProvider.dart';
import 'package:provider/provider.dart';
import 'package:sticky_headers/sticky_headers.dart';

class AtFriendPage extends StatelessWidget {
  const AtFriendPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double rpx = MediaQuery.of(context).size.width / 750;
    ScrollController controller = ScrollController();

    return Scaffold(
      appBar: AppBar(
          backgroundColor: Color(0xff121319),
          leading: FlatButton(
            child: IconButton(
              icon: Icon(
                Icons.close,
                color: Colors.white,
              ),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          title: Text("@好友"),
          bottom: PreferredSize(
            preferredSize: Size.fromHeight(80 * rpx),
            child: Container(
              decoration: BoxDecoration(color: Colors.grey[700]),
              margin: EdgeInsets.symmetric(horizontal: 20 * rpx),
              padding: EdgeInsets.symmetric(horizontal: 20 * rpx),
              child: TextField(
                decoration: InputDecoration(
                    icon: Icon(
                      Icons.search,
                      color: Colors.grey[400],
                    ),
                    hintText: "搜索用户备注或名字",
                    hintStyle: TextStyle(color: Colors.grey[500])),
              ),
            ),
          )),
      body: genUserList(context, controller),
    );
  }
}

genUserList(context, ScrollController controller) {
  AtUserProvider provider = Provider.of(context);
  // List<String> groupList = provider.groupList;

  return ListView.builder(
    shrinkWrap: true,
    controller: controller,
    itemCount: data.length,
    itemBuilder: (BuildContext context, int index) {
      return StickyHeader(
        header: Container(
          width: MediaQuery.of(context).size.width,
          height: 50,
          decoration: BoxDecoration(color: Colors.black),
          child: Text(
            provider.groupList[index],
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
        ),
        // content: genContentList(
        //     provider.result[provider.groupList[index]], context, controller),
        content: Container(),
      );
    },
  );
}

genContentList(
    List<dynamic> friends, BuildContext context, ScrollController controller) {
  double rpx = MediaQuery.of(context).size.width / 750;
  ListView.builder(
      shrinkWrap: true,
      controller: controller,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          height: 100 * rpx,
          child: Row(
            children: [
              Container(
                width: 100 * rpx,
                child: CircleAvatar(
                  backgroundImage: NetworkImage(friends[index]["avatarUrl"]),
                ),
              ),
              Container(
                width: 500 * rpx,
                child: Column(children: [
                  Container(
                    child: Text(friends[index]["userName"]),
                  ),
                  friends[index]["desc"].toString().length == 0
                      ? SizedBox()
                      : Container(
                          child: Text(friends[index]["desc"]),
                        ),
                ]),
              ),
              Container(
                width: 150 * rpx,
              )
            ],
          ),
        );
      });
}
